package Year22_Month06.day18;

public class 合并二叉树_方法一 {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        return rebuildTree(root1,root2);
    }

    public TreeNode rebuildTree(TreeNode node1,TreeNode node2){
        if(node1 == null && node2 == null) return null;
        int val = 0;
        if(node1 != null) val += node1.val;
        if(node2 != null) val += node2.val;
        TreeNode root = new TreeNode(val);
        //这里要注意，两棵树的同侧子树如果其中一个为空时候的判定
        root.left = rebuildTree(node1 == null ? null : node1.left,node2 == null ? null : node2.left);
        root.right = rebuildTree(node1 == null ? null : node1.right,node2 == null ? null : node2.right);
        return root;
    }
}
